// pages/msg_staff/msg_staff.js
let app = getApp();


Page({

    /**
     * 页面的初始数据
     */
    data: {
        list:[],
        xianshi: false,
        curtrue: true,
        option: false,
        loadModal: true,
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        hidden: true,
        searchdata: [],
        employeedata: [{
                'lid': '001',
                'lrealname': '测试姓名',
                'depName': "测试部门",
                'lphone': 18546592452,
            },
            {
                'lid': '002',
                'lrealname': '测试姓名',
                'depName': "测试部门",
                'lphone': 18546592452,
            },
            {
                'lid': '003',
                'lrealname': '测试姓名',
                'depName': "测试部门",
                'lphone': 18546592452,
            },
        ]
    },
    //输入框的确定搜索
    bindconfirm: function (e) {
        var that = this
        console.log('获取到的输入框中的id：', e.currentTarget.dataset.id)
        // 货品信息页面的搜索
        if (that.data.searchdata == '') {
            that.setData({
                searchdata: that.data.employeedata
            })
        }
        if (e.detail.value == '') {
            that.setData({
                employeedata: that.data.searchdata
            })
        } else {
            var data = that.data.employeedata
            let s = e.detail.value

            function aa() {
                let _this = that
                let str = data
                console.log(s)
                let searchid = [];
                let sear = new RegExp(s);
                for (var j = 0; j < str.length; j++) {
                    if (sear.test(str[j].lrealname)) {
                        searchid.push(str[j]);
                        console.log(searchid)
                    }
                }
                console.log(searchid)
                if (searchid != '') {
                    _this.setData({
                        employeedata: searchid
                    })
                    wx.showToast({
                        title: '搜索成功',
                        icon: 'success',
                        duration: 1000
                    })
                } else {
                    wx.showToast({
                        title: '未搜索到相关信息',
                        icon: 'none',
                        duration: 2000,
                        mask: 'false'
                    })
                    _this.setData({
                        employeedata: searchid
                    })
                }
            };
            aa();
        }
        console.log('搜索后赋值的列表：', that.data.goodsdata)

    },
    scan1() { // 添加员工信息
        wx.navigateTo({
            url: '../msg_staff_add/msg_staff_add'
        })
    },
    jump(e) {
        wx.navigateTo({
            url: '../msg_staff_details/msg_staff_details?lid=' + e.currentTarget.dataset.lid
        })
    },
    jump1(e) {
        wx.navigateTo({
            url: '../msg_canvas/msg_canvas?code=' + e.currentTarget.dataset.lid
        })
    },
    //打电话按钮
    bindphone: function (e) {
        console.log('获取到点击的电话号码', e.currentTarget.dataset.phone)
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone
        })
    },
    //获取文字信息
    getCur(e) {
        this.setData({
            hidden: false,
            listCur: this.data.list[e.target.id].garname,
        })
    },
    setCur(e) {
        this.setData({
            hidden: true,
            listCur: this.data.listCur
        })
    },
    //滑动选择Item
    tMove(e) {
        let y = e.touches[0].clientY,
            offsettop = this.data.boxTop,
            that = this;
        //判断选择区域,只有在选择区才会生效
        if (y > offsettop) {
            let num = parseInt((y - offsettop) / 20);
            this.setData({
                listCur: that.data.list[num].garname
            })
        };
    },

    //触发全部开始选择
    tStart() {
        this.setData({
            hidden: false,
        })
    },
    //触发结束选择
    tEnd() {
        this.setData({
            hidden: true,
            listCurID: this.data.listCur
        })
    },
    indexSelect(e) {
        let that = this;
        let barHeight = this.data.barHeight;
        let list = this.data.list;
        let scrollY = Math.ceil(list.length * e.detail.y / barHeight);
        for (let i = 0; i < list.length; i++) {
            if (scrollY < i + 1) {
                that.setData({
                    listCur: list[i].garname,
                    movableY: i * 20
                })
                return false
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        app.globalData.userinfo = ''
        let list = [];
        // for (let i = 0; i < 26; i++) {
        //     list[i] = ({
        //         'garname': String.fromCharCode(65 + i),
        //         'info': []
        //     })
        // }
        // this.setData({
        //     list: list,
        //     listCur: list[0].garname
        // })
        // console.log(this.data.list)
        // // console.log(this.data.list)
        // for (var i = 0; i < this.data.list.length; i++) {
        //     if (this.data.list[i].garname != 'V') {
        //         this.data.list[i]['info'] = []
        //     } else {
        //         this.data.list.splice(i, 1)
        //     }
        // }
        // this.setData({
        //     list: this.data.list,
        // })
        wx.request({
            url: 'https://' + app.globalData.url + '/staff/findStaffByMobile',
            data: {
                mobile: app.globalData.user_phone
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/json;charset=utf-8'
            },
            success: (res) =>{
                if (res.data.success == true) {
                    if (res.data.obj != '' && res.data.obj != null) {
                        that.setData({
                            list: res.data.obj
                        })
                        // for (var i = 0; i < that.data.list.length; i++) {
                        //     for (var j = 0; j < that.data.table.length; j++) {
                        //         if (that.data.table[j].LQUICKQUERY2 == that.data.list[i].garname) {
                        //             that.data.list[i].info.push(that.data.table[j])
                        //             that.setData({
                        //                 list: that.data.list
                        //             })
                        //         }
                        //     }
                        // }
                        // for (var i = 0; i < that.data.list.length; i++) {
                        //     if (that.data.list[i].info == '') {
                        //         that.data.list.splice(i, 1)
                        //         that.setData({
                        //             list: that.data.list
                        //         })
                        //     }
                        // }
                        console.log(this.data.list)

                        that.setData({
                            loadModal: false,
                            option: true,
                            listCur: this.data.list[0].garname
                        })
                        wx.createSelectorQuery().select('.indexBar-box').boundingClientRect(function (res) {
                            that.setData({
                                boxTop: res.top
                            })
                        }).exec();
                        wx.createSelectorQuery().select('.indexes').boundingClientRect(function (res) {
                            that.setData({
                                barTop: res.top
                            })
                        }).exec()
                    } else {
                        that.setData({
                            loadModal: false,
                            xianshi: true,
                            option:false
                        })
                        // wx.showModal({
                        //     title: '提示',
                        //     content: '当前数据为空',
                        //     showCancel: false,
                        //     confirmText: '确定',
                        //     success(res) {
                        //         if (res.confirm) {

                        //         }
                        //     }
                        // })
                    }

                } else {
                    that.setData({
                        loadModal: false,
                        xianshi: true
                    })
                    wx.showModal({
                        title: '提示',
                        content: '获取数据失败',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                }
            },
            fail: function (res) {
                that.setData({
                    loadModal: false,
                    xianshi: true
                })
                wx.showModal({
                    title: '提示',
                    content: '访问服务器出错',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            },
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        // let that = this;
        // wx.createSelectorQuery().select('.indexBar-box').boundingClientRect(function (res) {
        //     that.setData({
        //         boxTop: res.top
        //     })
        // }).exec();
        // wx.createSelectorQuery().select('.indexes').boundingClientRect(function (res) {
        //     that.setData({
        //         barTop: res.top
        //     })
        // }).exec()
        // this.setData({
        //     loadModal:false
        // })
        // console.log('guanbile')
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (app.globalData.userinfo != '') {
            this.data.employeedata.push(app.globalData.userinfo)
            this.setData({
                employeedata: this.data.employeedata
            })
            console.log(this.data.employeedata)
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})